<?php
// +----------------------------------------------------------------------
// | zhanshop-marketing / Adlisten.php    [ 2024/12/23 16:54 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\admin\v1\service\finder;

use app\provider\AdEventUploadProvider;
use zhanshop\App;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Adlisten extends BaseFinder
{
    protected $tabs = [
        [
            'title' => '全部应用',
            'where' => [
                'app',
                '=',
                ''
            ],
        ],
    ];

    protected $headToolbar = [];

    protected $rowToolbar = [
        [
            'event' => 'open',
            'ico' => '&#xe642;',
            'title' => '事件上报',
            'method' => '',
            'page' => './page/ad/listen-upload.html',
        ],
    ];

    public function __construct(string $menuId)
    {
        parent::__construct($menuId);
        $apps = App::database()->model("ad_app")->order('create_time asc')->limit(20)->select();
        foreach ($apps as $app){
            $this->tabs[] = [
                'title' => $app['name'],
                'where' => [
                    'app',
                    '=',
                    $app['app']
                ],
            ];
        }
    }

    public function searchfrom(Request $request, Response $response){
        $data = parent::searchfrom($request, $response);
        $tabs = $this->tabs;
        unset($tabs[0]);
        $apps = [];
        foreach ($tabs as $tab){
            $app = $tab['where'][2];
            $apps[$app] = $tab['title'];
        }
        $data['app']['value'] = $apps;
        return $data;
    }


    public function adUpload(Request $request, Response $response)
    {
        $input = $request->validateData($request->get(), [
            'pk' => 'Required',
        ])->getData();
        $data = App::database()->table('ad_record')->where(['id' => $input['pk']])->findOrFail();
        $platform = $data['platform'];
        $result = [0, ""];
        $events = $request->post('event');
        foreach($events as $k => $event){
            if($event == 'activate'){
                if(isset($data['app']) && $data['app']){
                    $result = App::make(AdEventUploadProvider::class)->upload($data['app'], 'all', $event, $data, ['id' => $input['pk']]);
                    unset($events[$k]);
                }
            }
        }

        if($result[0] == false){
            App::error()->setError("激活上报失败，请检查是否勾选激活上报");
        }

        $record = App::database()->model("ad_upload")->where(['id' => $result[0]])->find();

        $platform = $record['platform'];
        foreach($events as $event){
            // 在激活的基础上去做后续的事件上报
            if($record){
                $platform = $record['platform'];
                App::make(AdEventUploadProvider::class)->$platform($data['app'], $event, $record['mate_type'], ['id' => $result[0]], $record, []);
            }
        }
        return [];
    }
}